package com.haozhen.component;

import com.haozhen.constant.ZkInfo;
import com.haozhen.entity.DataSourceLinkInfo;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.serialize.SerializableSerializer;

/**
 * @author haozhen
 * @email haozh@ync1.com
 * @date 2020/10/24  0:54
 * 用户初始化 或修改 zk节点信息
 */
public class DataSourceChangeServer {

    private ZkClient zkClient;

    public DataSourceChangeServer initZkClient(){
        zkClient = new ZkClient(ZkInfo.zkServer);
        zkClient.setZkSerializer(new SerializableSerializer());
        return this;
    }

    public DataSourceChangeServer registerDataSource(DataSourceLinkInfo dataSourceLinkInfo){
        boolean exists = zkClient.exists(ZkInfo.zkNode);
        if(!exists){
            zkClient.createPersistent(ZkInfo.zkNode,dataSourceLinkInfo);
        }else{
            zkClient.writeData(ZkInfo.zkNode,dataSourceLinkInfo);
        }
        return this;
    }


}
